<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Express_Plan extends ORM
{

    /**
     * @function 获取模板的详情收费计划
     * @param $templateid
     */
    public static function get_template_planlist($templateid)
    {
        if(!$templateid)
        {
            return false;
        }
        $planlist = DB::select()->from('express_plan')
            ->where('templateid','=',$templateid)->execute()->as_array();
        foreach ($planlist as &$p)
        {
            if($p['provincelist'])
            {
                $p['province_titlelist'] = Model_Express_Destination::get_address_title($p['provincelist']);
            }
            if($p['citylist'])
            {
                $p['city_titlelist'] = Model_Express_Destination::get_address_title($p['citylist']);
            }
            $p['start_price'] =  Currency_Tool::price($p['start_price']);
            $p['continue_price'] =  Currency_Tool::price($p['continue_price']);
        }
        return $planlist;
    }


    /**
     * @function 获取配送地址相差的快递计划,即计价类型
     * @param $freight_id  express_template的ID
     * @param $addressid   会员目的地ID
     * @return array
     */
    public static function get_plan($freight_id,$addressid)
    {
        $address = DB::select('*')
            ->from('member_address')->where('id','=',$addressid)
            ->execute()->current();
        return self::get_plan_by_address($freight_id,$address['province'],$address['city']);
    }

    /**
     * @param $freight_id
     */
    public static function get_plan_by_address($freight_id,$province,$city)
    {

        if(!$freight_id)
        {
            return array('status'=>0,'msg'=>'您所选的地址不在配送范围,请重新选择!');
        }
        //省id
//        print_r($province);
//        echo "---------\n";
//        print_r($city);die;
        $provinceid = DB::select('id')->from('express_destination')
            ->where('cityname','like','%'.str_replace('省','',$province).'%')->execute()->get('id');
        //城市id
        $cityid = DB::select('id')->from('express_destination')
            ->where('cityname','like','%'.str_replace('市','',$city).'%')->execute()->get('id');


        if(!$provinceid||!$cityid)
        {
            return array('status'=>0,'msg'=>'您所选的地址不在配送范围,请重新选择!');
        }
        $price = 0 ;//运费
        $sql ="select a.*,b.type from sline_express_plan as a inner join   sline_express_template as b
 on a.templateid=b.id  WHERE b.id=$freight_id and find_in_set($provinceid,a.provincelist) and find_in_set($cityid,a.citylist) limit 1";


        $plan = DB::query(Database::SELECT,$sql)->execute()->current();

        //如果有产品没有运费模板，那么不能下单
        if(empty($plan))
        {
            return array('status'=>0,'msg'=>'您所选的地址不在配送范围,请重新选择!');
        }
        return array('status'=>1,'msg'=>'地址正确','data'=>$plan);
    }


    /**
     * @function 获取单个产品的运费
     * @param $suit_info
     * @param $plan
     */
    public static function get_freight_price($suit_info,$plan,$num=1)
    {

        $price = 0 ;//运费
        switch ($plan['type'])
        {
            case 1://计重
                $weight = $suit_info['weight']*$num;
                //是否超过首重

                if($weight>$plan['start_num']&&$plan['continue_num']>0)
                {
                    $price = $plan['start_price']+ceil(($weight-$plan['start_num'])/$plan['continue_num'])*$plan['continue_price'];
                }
                else
                {
                    $price = $plan['start_price'];
                }
                break;
            case 2:
            //计  $number = $suit_info['number']*$num;
                if($num>$plan['start_num']&&$plan['continue_num']>0)
                {
                    $price = $plan['start_price']+ceil(($num-$plan['start_num'])/$plan['continue_num'])*$plan['continue_price'];

                }
                else
                {
                    $price = $plan['start_price'];
                }
                break;
            case 3://体积
                $bulk = $suit_info['bulk']*$num;
                if($num>$plan['start_num']&&$plan['continue_num']>0)
                {
                    $price = $plan['start_price']+ceil(($bulk-$plan['start_num'])/$plan['continue_num'])*$plan['continue_price'];
                }
                else
                {
                    $price = $plan['start_price'];
                }
                break;
        }
        return round($price,2);
    }

}